<?
require_once('version.inc');
require_once('dbaccess.inc');
require_once('language.inc');
require_once('header.inc');
require_once('crypt.inc');
require_once('settings.inc');
require_once('menu.inc');

function SaveNewLand() {
	$kl_num = $_REQUEST['kl_num'];
	$kl_land = $_REQUEST['kl_land'];

	$db = OpenDB();
	$query = "select kl_land from key_land where kl_num = $kl_num";
	$result = QueryDB($db, $query);
	$numrows = numrowsDB($result);

	if ($numrows == 1) {
	   Error(etMessage($db, 231, "Schl&uuml;ssel %d ist bereits vorhanden!", $kl_num));
	   closeDB($db);
	   return false;
	}

	$query = "insert into key_land (kl_num, kl_land) values ($kl_num, '$kl_land')";

	if (!TQueryDB($db, $query)) {
	   closeDB($db);
	   return false;
	}

	closeDB($db);
	return true;
}

function SaveEditLand() {
	$kl_num = $_REQUEST['kl_num'];
	$kl_land = $_REQUEST['kl_land'];

	$db = OpenDB();
	$query = "select kl_land from key_land where kl_num = $kl_num";
	$result = QueryDB($db, $query);
	$numrows = numrowsDB($result);

	if ($numrows != 1) {
	   Error(GetMessage($db, 232, "Schl&uuml;ssel %d ist nicht vorhanden!", $kl_num));
	   closeDB($db);
	   return false;
	}

	$query = "update key_land set kl_land = '$kl_land' where kl_num = $kl_num";

	if (!TQueryDB($db, $query)) {
	   Error("SQL-Error: $query");
	   closeDB($db);
	   return false;
	}

	closeDB($db);
	return true;
}

$newland = $_REQUEST['newland'];
$editland = $_REQUEST['editland'];

if (!isset($editland)) {
   $editland = 0;
}

$savenewland = $_REQUEST['savenewland'];
$saveeditland = $_REQUEST['saveeditland'];

if (isset($savenewland)) {
   SaveNewLand();
}

if (isset($saveeditland)) {
   SaveEditLand();
}

$nav = "headline=17&menu=$menu";
?>
<form name="land" action="land.php" method="post">
<input type="hidden" name="headline" value="17">
<input type="hidden" name="rstufe" value="<? echo "$rstufe"; ?>">
<input type="hidden" name="menu" value="<? echo "$menu"; ?>">
<input type="hidden" name="unum" value="<? echo "$unum"; ?>">
<table class="indent"><tr><td>
<?php
$db = OpenDB();
ButtonLink(GetMessage($db, 233, "Neues Land"), "land.php", "$nav&newland=newland");
echo "</td></tr></table>\n";

$query = "select kl_num, kl_land from key_land order by kl_num";
$result = QueryDB($db, $query);

if (!$result) {
   echo "</table></form><br>\n";
   require('footer.inc');
   exit;
}

$numrows = numrowsDB($result);

if ($numrows > 0 || isset($newland)) {
?>
<table class="sel">
   <tr>
      <th class="sel">&nbsp;</th>
      <th class="sel"><? Output($db, 158, "Key"); ?></th>
      <th class="sel"><? Output($db, 159, "Text"); ?></th>
   </tr>
<?php
   $row = 0;

   while ($row < $numrows) {
      $data = fetchDB($result, $row);
      $kl_num = $data[0];
      $kl_land = $data[1];

      if ($kl_num == $editland) {
         echo "<input type=\"hidden\" name=\"kl_num\" value=\"$kl_num\">\n";
         echo "<tr><td class=\"selakt\"><button type=\"submit\" name=\"saveeditland\" value=\"$kl_num\"><img src=\"image/new.png\" border=0 alt=\"Edit\"></button></td>";
         echo "<td class=\"selnum\">$kl_num</td>";
         echo "<td class=\"sel\"><input type=\"text\" name=\"kl_land\" value=\"$kl_land\" size=40 maxlength=50></td></tr>\n";
      } else {
         echo "<tr><td class=\"selakt\"><button type=\"submit\" name=\"editland\" value=\"$kl_num\"><img src=\"image/edit.png\" border=0 alt=\"Edit\"></button></td>";
         echo "<td class=\"selnum\">$kl_num</td><td class=\"sel\">$kl_land</td></tr>";
      }

      $row++;
   }

   if (isset($newland)) {
      echo "<tr><td class=\"selakt\"><button type=\"submit\" name=\"savenewland\" value=\"savenewland\"><img src=\"image/new.png\" border=0 alt=\"Neu\"></button></td>";
      echo "<td class=\"sel\"><input type=\"text\" name=\"kl_num\" size=7 maxlength=7></td>";
      echo "<td class=\"sel\"><input type=\"text\" name=\"kl_land\" size=40 maxlength=50></td></tr>\n";
   }
?>
</table>
<table class="indent"><tr><td>
<? ButtonLink(GetMessage($db, 233, "Neues Land"), "land.php", "$nav&newland=newland"); ?>
</td></tr></table>
</form>
<?php
}

require('footer.inc');
?>
